using System;
using System.Net;
using System.Threading;
namespace SilverSea.Video.Tools
{
	public class PTZSource
	{
		private string string_0;
		private string string_1;
		private string string_2;
		private bool bool_0;
		private bool bool_1 = true;
		private int int_0 = 5000;
		private Thread thread_0;
		private ManualResetEvent manualResetEvent_0;
		public string Source
		{
			get
			{
				return this.string_0;
			}
			set
			{
				this.string_0 = value;
			}
		}
		public string Login
		{
			get
			{
				return this.string_1;
			}
			set
			{
				this.string_1 = value;
			}
		}
		public string Password
		{
			get
			{
				return this.string_2;
			}
			set
			{
				this.string_2 = value;
			}
		}
		public bool SeparateConnectionGroup
		{
			get
			{
				return this.bool_0;
			}
			set
			{
				this.bool_0 = value;
			}
		}
		public bool PreventCaching
		{
			get
			{
				return this.bool_1;
			}
			set
			{
				this.bool_1 = value;
			}
		}
		public int RequestTimeout
		{
			get
			{
				return this.int_0;
			}
			set
			{
				this.int_0 = value;
			}
		}
		public bool Running
		{
			get
			{
				if (this.thread_0 != null)
				{
					if (!this.thread_0.Join(0))
					{
						return true;
					}
					this.method_0();
				}
				return false;
			}
		}
		public PTZSource()
		{
		}
		public PTZSource(string source)
		{
			this.string_0 = source;
		}
		public void Start()
		{
			if (this.thread_0 == null && !this.Running)
			{
				this.manualResetEvent_0 = new ManualResetEvent(false);
				this.thread_0 = new Thread(new ThreadStart(this.WorkerThread));
				this.thread_0.Name = this.string_0;
				this.thread_0.Start();
			}
		}
		public void SignalToStop()
		{
			if (this.thread_0 != null)
			{
				this.manualResetEvent_0.Set();
			}
		}
		public void WaitForStop()
		{
			if (this.thread_0 != null)
			{
				this.thread_0.Join();
				this.method_0();
			}
		}
		public void Stop()
		{
			if (this.Running)
			{
				this.thread_0.Abort();
				this.WaitForStop();
			}
		}
		private void method_0()
		{
			this.thread_0 = null;
			this.manualResetEvent_0.Close();
			this.manualResetEvent_0 = null;
		}
		public void WorkerThread()
		{
			HttpWebRequest httpWebRequest = null;
			WebResponse webResponse = null;
			Random random = new Random((int)DateTime.Now.Ticks);
			try
			{
				if (!this.bool_1)
				{
					httpWebRequest = (HttpWebRequest)WebRequest.Create(this.string_0);
				}
				else
				{
					httpWebRequest = (HttpWebRequest)WebRequest.Create(string.Concat(new object[]
					{
						this.string_0,
						(this.string_0.IndexOf('?') == -1) ? '?' : '&',
						"fake=",
						random.Next().ToString()
					}));
				}
				httpWebRequest.Timeout = this.int_0;
				if (this.string_1 != null && this.string_2 != null && this.string_1 != string.Empty)
				{
					httpWebRequest.Credentials = new NetworkCredential(this.string_1, this.string_2);
				}
				if (this.bool_0)
				{
					httpWebRequest.ConnectionGroupName = this.GetHashCode().ToString();
				}
				webResponse = httpWebRequest.GetResponse();
			}
			catch (WebException)
			{
				Thread.Sleep(250);
			}
			catch (Exception)
			{
			}
			finally
			{
				if (httpWebRequest != null)
				{
					httpWebRequest.Abort();
					httpWebRequest = null;
				}
				if (webResponse != null)
				{
					webResponse.Close();
					webResponse = null;
				}
			}
		}
	}
}
